from rest_framework import serializers

from .models import *

# EventImg EventType Comment Event

# EventImg 事件图片模型对应序列化器，提供给事件序列化器使用
# 事件图片序列化器
class EventImgSerializer(serializers.ModelSerializer):
    """
    事件图片序列化器
    """
    created_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    last_edited_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    class Meta:
        model = EventImg
        fields = "__all__"
# 事件图片下拉选序列化
class EventImgOptionSerializer(serializers.ModelSerializer):
    """
    事件图片下拉选序列化
    """
    class Meta:
        model = EventImg
        fields = ['id','event_img']


# EventType 事件类型管理序列化器，用于创建事件时添加类别，方便进行按类别搜索事件
# 事件类型序列化器
class EventTypeSerializer(serializers.ModelSerializer):
    """
    事件类型序列化器
    """
    created_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    last_edited_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    class Meta:
        model = EventType
        fields = "__all__"
# 事件类型下拉选序列化
class EventTypeOptionSerializer(serializers.ModelSerializer):
    """
    事件类型下拉选序列化
    """
    class Meta:
        model = EventType
        fields = ['id','name']


# Comment 评论模型对应序列化器
# 评论序列化器
class CommentSerializer(serializers.ModelSerializer):
    """
    评论序列化器
    """
    created_by_name = serializers.CharField(source='created_by.username',read_only=True)
    last_edited_by_name = serializers.CharField(source='last_edited_by.username',read_only=True)
    created_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    last_edited_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    class Meta:
        model = Comment
        fields = "__all__"
# 评论下拉选序列化器
class CommentOptionSerializer(serializers.ModelSerializer):
    """
    评论下拉选序列化器
    """
    class Meta:
        model = Comment
        fields = ['id','news']



#动态路由序列化子模块
# 获取事件图片子模块
class ImgInEvent(serializers.ModelSerializer):
    """获取事件图片子模块"""
    img = serializers.SerializerMethodField()
    class Meta:
        model = Event
        fields = ['img']
    def get_img(self,obj):
        imgs =  EventImg.objects.filter(Event=obj,deleted='0')
        if imgs:
            Img = []
            for factor in imgs:
                Img.append(EventImgOptionSerializer(factor).data)
            return Img
# 获取事件评论子模块
class CommentInEvent(serializers.ModelSerializer):
    """获取事件评论子模块"""
    comment = serializers.SerializerMethodField()
    class Meta:
        model = Event
        fields = ['comment']
    def get_img(self,obj):
        comments =  Comment.objects.filter(Event=obj,deleted='0')
        if comments:
            Commentdata = {}
            for factor in comments:
                Commentdata.append(CommentSerializer(factor).data)
            return Commentdata

# Event 事件序列化器，展现给用户看到的主体功能
# 事件序列化器
class EventSerializer(serializers.ModelSerializer):
    """
    事件序列化器
    """
    # 用户头像
    portrait = serializers.ImageField(source='created_by.portrait.portrait',read_only=True)
    # 用户名
    created_by_name = serializers.CharField(source='created_by.username',read_only=True)
    # 事件类别
    eventType_name = serializers.CharField(source='eventType.name',read_only=True)
    # 发起时间
    created_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    # 事件时间
    start_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    end_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    # 事件图片
    imgs = ImgInEvent(many=True)
    # 事件评论
    comment = CommentInEvent(many=True)
    # 修改时间
    last_edited_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    class Meta:
        model = Event
        fields = "__all__"
